package com.legal.app.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author Ale
 *
 */
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public RestTemplate restTemplate() {
        // 创建基础的HTTP请求工厂
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(5000); // 设置连接超时时间
        requestFactory.setReadTimeout(5000);    // 设置读取超时时间

        // 使用缓冲请求工厂支持多次读取响应
        BufferingClientHttpRequestFactory bufferedFactory =
                new BufferingClientHttpRequestFactory(requestFactory);

        // 创建RestTemplate实例并设置请求工厂
        RestTemplate restTemplate = new RestTemplate(bufferedFactory);

        // 自定义错误处理器（可选）
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler());

        return restTemplate;
    }

    @Autowired
    private AvatarConfig avatarConfig;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String uploadDir = avatarConfig.getUploadDir();
        String absolutePath = System.getProperty("user.dir") + File.separator + uploadDir;

        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:F:/JavaWEB/code2/code/legal-aid-wxapp/static/images/avatar");
    }

}